﻿using Common;
using STMS.BLL;
using STMS.Models.DModels;
using STMS.Models.VModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Product
{
    public partial class FrmProductInStore : Form
    {
        public FrmProductInStore()
        {
            InitializeComponent();
        }

        ProductBLL proBLL = new ProductBLL();
        StoreRegionBLL sRegionBLL = new StoreRegionBLL();


        /// <summary>
        /// 产品管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProductList_Click(object sender, EventArgs e)
        {
            FrmProductList fProList = new FrmProductList();
            Panel panel = this.Parent as Panel;
            panel.AddPanelPage(fProList);
        }



        private void FrmProductInStore1_Load(object sender, EventArgs e)
        {
            InitCboProductList();//加载产品列表
            cboStores.LoadStores();//加载仓库列表
            txtInCount.Value = 0;
            dgvProStoreList.DataSource = null;
            lblTypeName.Text = "入库记录：";
        }

        private void InitCboProductList()
        {
            List<ProductInfo> products = proBLL.GetAllProducts();
            products.Insert(0, new ProductInfo()
            {
                ProductId = 0,
                ProductName = "----请选择产品----"
            });
            cboProducts.DisplayMember = "ProductName";
            cboProducts.ValueMember = "ProductId";
            cboProducts.DataSource = products;
            cboProducts.SelectedIndex = 0;
        }

        private void cboStores_SelectedIndexChanged(object sender, EventArgs e)
        {
            int storeId = cboStores.SelectedValue.GetInt();
            if (storeId > 0)
            {
                List<StoreRegionInfo> regions = sRegionBLL.GetRegionsByStoreId(storeId);
                cboSRegions.DisplayMember = "SRegionName";
                cboSRegions.ValueMember = "SRegionId";
                cboSRegions.DataSource = regions;
                cboSRegions.SelectedIndex = 0;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            cboProducts.SelectedIndex = 0;
            cboStores.SelectedIndex = 0;
            txtInCount.Value = 0;
        }

        private void btnInStore_Click(object sender, EventArgs e)
        {
            int productId = cboProducts.SelectedValue.GetInt();
            int storeId = cboStores.SelectedValue.GetInt();
            int regionId = cboSRegions.SelectedValue.GetInt();
            int inCount = int.Parse(txtInCount.Value.ToString());
            string msgTitle = "产品入库";
            bool blCheck = CheckInStoreInfo(productId, regionId, inCount);
            if (!blCheck) return;

            ProductStoreInfo psInfo = new ProductStoreInfo()
            {
                ProductId = productId,
                StoreId = storeId,
                SRegionId = regionId,
                ProductCount = inCount
            };

            bool bl = proBLL.InStoreProducts(psInfo);
            if (bl)
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"产品：{cboProducts.Text} 入库成功！");
                ShowInStoreRecords(cboProducts.Text, cboStores.Text, cboSRegions.Text, inCount);
            }

        }

        /// <summary>
        /// 添加本次入库记录显示到记录列表中（这里不入库，因为入库记录已存储到数据库）
        /// </summary>
        /// <param name="proName"></param>
        /// <param name="storeName"></param>
        /// <param name="regionName"></param>
        /// <param name="inCount"></param>
        private void ShowInStoreRecords(string proName, string storeName, string regionName, int inCount)
        {
            btnProInStoreRecords.BackColor = Color.FromArgb(50, 100, 255);
            btnProductList.BackColor = Color.FromArgb(50, 122, 200);
            if (records == null)
                records = new List<ViewProductStoreInfo>();
            records.Add(new ViewProductStoreInfo()
            {
                Id = records.Count + 1,
                ProductName = proName,
                StoreName = storeName,
                SRegionName = regionName,
                ProductCount = inCount
            });
            dgvProStoreList.AutoGenerateColumns = false;
            dgvProStoreList.DataSource = records;
        }

        private bool CheckInStoreInfo(int productId, int regionId, int inCount)
        {
            string msgTitle = "产品入库";
            if (productId == 0)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "请选择要入库的产品！");
                return false;
            }
            if (regionId == 0)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "请选择要入货的仓库分区！");
                return false;
            }
            if (inCount <= 0)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "请输入要入库的数量！");
                return false;
            }
            return true;
        }



        private void btnProInStoreRecords_Click(object sender, EventArgs e)
        {

            lblTypeName.Text = "入库记录：";
            dgvProStoreList.AutoGenerateColumns = false;
            dgvProStoreList.DataSource = records;
            btnProInStoreRecords.BackColor = Color.FromArgb(50, 100, 255);
            btnProStoreList.BackColor = Color.FromArgb(50, 122, 200);

        }

        List<ViewProductStoreInfo> records = new List<ViewProductStoreInfo>();

        private void btnProStoreList_Click(object sender, EventArgs e)
        {
            lblTypeName.Text = "商品库存数据：";
            List<ViewProductStoreInfo> list = proBLL.GetProductStoreList(0, 0, 0);
            dgvProStoreList.AutoGenerateColumns = false;
            dgvProStoreList.DataSource = list;
            btnProStoreList.BackColor = Color.FromArgb(50, 100, 255);
            btnProInStoreRecords.BackColor = Color.FromArgb(50, 122, 200);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmProductInStore1_Load(this, e);
        }
    }
}
